<?php

/**
 * Created by PhpStorm.
 * User: M2
 * Date: 13-11-18
 * Time: 下午2:13
 */
class YlNavigationTaglib {

    public function __construct() {
        C(include SITE_PATH . '/extends/taglib/tagarr.php');
    }

    public function parse($content, $sid, $cateid, $contentid, $domain, $channel) {
        if (stripos($content, 'yl:navigation') !== false) {
            $matches = preg_parse($content, 'yl:navigation');
            foreach ($matches as $match) {
                $type = getParseOneAttr($match, 'type');
                $tiptext = getParseOneAttr($match, 'tipText');
                $emptytext = getParseOneAttr($match, 'emptyText');
                $class = getParseOneAttr($match, 'class');
                if (!empty($type)) {
                    $type = $type;
                }
                if (!empty($tiptext)) {
                    $tipText = $tiptext;
                }
                if (!empty($emptytext)) {
                    $emptyText = $emptytext;
                }
                if (!empty($class)) {
                    $class = $class;
                }

                switch (strtolower($type)) {
                    case 'previouscontent' :
                        $data['id'] = array('lt', $contentid);
                        $order = 'id desc';
                        break;
                    case 'nextcontent' :
                        $data['id'] = array('gt', $contentid);
                        $order = 'id asc';
                        break;
                    case 'previouschannel' :
                        $data['id'] = array('lt', $cateid);
                        $order = 'id desc';
                        break;
                    case 'nextchannel' :
                        $data['id'] = array('gt', $cateid);
                        $order = 'id asc';
                        break;
                }
                $data['siteid'] = $sid;

                if (strtolower($type) == "previouscontent" || strtolower($type) == "nextcontent") {
                    if (($clist = S('cache_channel_' . $cateid)) === false) {
                        $clist = M('cms_category')->where('id=' . $cateid)->find();
                        S('cache_channel_' . $cateid, $clist);
                    }
                    $contentartnaming = $channel['artnaming'];
                    if (!empty($clist['artnaming'])) {
                        $contentartnaming = $clist['artnaming'];
                    }
                    $res = M('cms_article')
                                    ->field('(case when url <> "" then url else
                                     (concat("' . $domain . '","/",replace(replace("' . $contentartnaming . '","{cateid}",
                                     cid),"{contentid}",id))) end) as url,replace(imageurl,"@","' . $domain . '")
                                     as imageurl,replace(big_imageurl,"@","' . $domain . '") as big_imageurl,title,subtitle,author,updatetime,inputtime,tags,
                                     url as articleUrl,source,content,summary,group_name_collection,readpoint,keywords,description,create_uid,create_uname,update_uid,
                                    update_uname,fileurl,cid,id,siteid')
                                    ->order($order)
                                    ->where($data)->find();
                    $url = $res['url'];
                    $name = $res['title'];
                    $type = '1';
                } else if (strtolower($type) == "previouschannel" || strtolower($type) == "nextchannel") {
                    $res = M('cms_category')
                                    ->field('(case when url <> "" then url when createurl <> "" then createurl
					                else (concat("' . $domain . '","/",replace(replace((case when catenaming <> ""
	                		 then catenaming else ("' . $channel['catenaming'] . '") end),"{cateid}",id),"{@ChannelID}",id))) end) as cateurl,
					                replace(imageurl,"@","' . $domain . '") as imageurl,id,cname,content,keywords,
					                description,catetemplate,arttemplate,createurl,catenaming,artnaming,
					                inputtime,updatetime,content_num,pid')
                                    ->order($order)
                                    ->where($data)->find();
                    $url = $res['cateurl'];
                    $name = $res['cname'];
                    $type = '4';
                }

                $text = getParseContentText($match, 'yl:navigation');
                if (!empty($text)) {
                    if ($name) {
                        $text = getParseContentText($text, 'yl:successtemplate');
//                        $text = taglib(C('yl:successtemplate'))->parse($text, $res, $type);
                    } else {
                        $text = getParseContentText($text, 'yl:failuretemplate');
//                        $text = taglib(C('yl:failuretemplate'))->parse($text, $res, $type);
                    }
                    $text = taglib(C('yl:a'))->parse($text, '', $res, $type, $domain, $channel);
                    $parseStr = $tipText . $text;
                } else {
                    if ($name) {
                        $parseStr = $tipText . '<a href="' . $url . '" class="' . $class . '">' . $name . '</a>';
                    } else {
                        $parseStr = $tipText . $emptyText;
                    }
                }
                $content = str_replace($match, $parseStr, $content);
            }
        }
        return $content;
    }

}
